/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more detailsettings->
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "appearanceconfig.h"

#include "src/platform.h"
#include <QtGui/QLabel>
#include <QtGui/QComboBox>
#include <QtGui/QCheckBox>
#include <QtGui/QGroupBox>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QFont>
#include <QtGui/QApplication>
#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include <QtCore/QVariant>
#include <QtCore/QString>

//! default parameters

const bool AppearanceConfig::m_default_show_toolbar = true;
const bool AppearanceConfig::m_default_show_tray_icon = true;
const bool AppearanceConfig::m_default_start_minimized = false;


// TODO: get language from system locale
const QString AppearanceConfig::m_default_language = QString("English");



AppearanceConfig::AppearanceConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();    changeEvent(new QEvent(QEvent::LanguageChange));

    settings = new QSettings(Platform::getOrganizationName(),
                             Platform::getApplicationName());

    settings->beginGroup("Appearance");

    loadLanguages();

}

AppearanceConfig::~AppearanceConfig() {
    settings->endGroup();
    delete  languageLabel;
    delete languageComboBox;
    delete languageLayout;
    delete showToolbar;
    delete showTrayIcon;
    delete mainLayout;
}

bool AppearanceConfig::getShowToolbar() const {
    return showToolbar->isChecked();
}

bool AppearanceConfig::getShowTrayIcon() const {
    return showTrayIcon->isChecked();
}

bool AppearanceConfig::getStartMinimized() const {
    return startMinimized->isChecked();
}


QString AppearanceConfig::getLanguage() const {
    return languageComboBox->currentText();
}

void AppearanceConfig::loadLanguages() {
    QDir dir(Platform::getL10nPath());

    const QStringList translations =
            dir.entryList(QStringList() << ".ts");

     languageComboBox->addItems(translations);
}

void AppearanceConfig::save() {
    settings->setValue("showToolbar", getShowToolbar());
    settings->setValue("showTrayIcon", getShowTrayIcon());
    settings->setValue("startMinimized", getStartMinimized());
    settings->setValue("language", getLanguage());
}

void AppearanceConfig::read() {
    showToolbar->setChecked(settings->value("showToolbar",  m_default_show_toolbar).toBool());
    showTrayIcon->setChecked(settings->value("showTrayIcon", m_default_show_tray_icon).toBool());
    startMinimized->setChecked(settings->value("startMinimized", m_default_start_minimized).toBool());

    const QString lang = settings->value("language", m_default_language).toString();
    setLanguage(lang);
}

void AppearanceConfig::truncate() {
    showToolbar->setChecked(m_default_show_toolbar);
    showTrayIcon->setChecked(m_default_show_tray_icon);
    startMinimized->setChecked(m_default_start_minimized);
    setLanguage(m_default_language);
}


void AppearanceConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	languageLabel->setText(QApplication::tr("Application language"));
	showToolbar->setText(QApplication::tr("Show Toolbar"));
	showTrayIcon->setText(QApplication::tr("Show in tray"));
        startMinimized->setText(QApplication::tr("Start Minimized"));
    }
}

void AppearanceConfig::createUi() {
    languageLabel = new QLabel(this);
    languageComboBox = new QComboBox(this);
    languageLayout = new QHBoxLayout;


    languageLayout->addWidget(languageLabel);
    languageLayout->addWidget(languageComboBox);

    showToolbar = new QCheckBox(this);
    showTrayIcon = new QCheckBox(this);
    startMinimized = new QCheckBox(this);
    mainLayout = new QVBoxLayout;

     mainLayout->addLayout(languageLayout);

    mainLayout->addWidget(showToolbar);
    mainLayout->addWidget(showTrayIcon);
    mainLayout->addWidget(startMinimized);

    mainLayout->addStretch();
    this->setLayout(mainLayout);


}

void AppearanceConfig::setLanguage(const QString &lang) {
    for(int i = 0; i < languageComboBox->count(); i++) {
	if(languageComboBox->itemText(i) == lang)
	    languageComboBox->setCurrentIndex(i);
    }
}
